﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SurveyTracker.Data;
using SubSonic.Schema;
using System.Data;

namespace SurveyTracker.admin
{
    public partial class ImportEmails : System.Web.UI.Page
    {


        private static surveyTrackerDB GetDb()
        {
            surveyTrackerDB db = new surveyTrackerDB();
            return db;
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            surveyTrackerDB db = GetDb();
            if(!IsPostBack)
            {
                BindEmailList(db);
                BindEmailGrid(db);
                //StoredProcedure sproc = db.Get();
                //sproc.ExecuteScalar<int>();
            }
        }

        private void BindEmailGrid(surveyTrackerDB db)
        {
        
            int selectedEmailList = int.Parse(lstMailingLists.SelectedValue);
            
            StoredProcedure sproc = db.getEmailsFromList(selectedEmailList);
            DataSet dsEmails = sproc.ExecuteDataSet();
            
            grdEmailInformation.DataSource = dsEmails;
            grdEmailInformation.DataBind();
            
        }

        private void BindEmailList(surveyTrackerDB db)
        {
            StoredProcedure sproc = db.GetEmailLists();
            DataSet dsEmailLists = sproc.ExecuteDataSet();
            lstMailingLists.DataSource = dsEmailLists;
            lstMailingLists.DataTextField = "ListName";
            lstMailingLists.DataValueField = "ListId";
            
            lstMailingLists.DataBind();
        }
 
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            surveyTrackerDB db = GetDb();
            BindEmailGrid(db);
        }
    }
}
